package com.lframework.xingyun.promo.impl.coupon;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.utils.OpLogUtil;
import java.io.Serializable;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.web.core.utils.IdUtil;
import com.lframework.starter.common.utils.ObjectUtil;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.web.core.utils.PageHelperUtil;
import com.lframework.starter.common.utils.Assert;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.xingyun.promo.entity.MarketingCoupon;
import com.lframework.xingyun.promo.mappers.MarketingCouponMapper;
import com.lframework.xingyun.promo.service.coupon.MarketingCouponService;
import com.lframework.xingyun.promo.vo.coupon.CreateMarketingCouponVo;
import com.lframework.xingyun.promo.vo.coupon.QueryMarketingCouponVo;
import com.lframework.xingyun.promo.vo.coupon.UpdateMarketingCouponVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MarketingCouponServiceImpl extends BaseMpServiceImpl<MarketingCouponMapper, MarketingCoupon> implements MarketingCouponService {

    @Override
    public PageResult<MarketingCoupon> queryPage(Integer pageIndex, Integer pageSize, QueryMarketingCouponVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        IPage<MarketingCoupon> page = getBaseMapper().queryPage(vo);
        return PageResultUtil.convert(page);
    }

    @Override
    public List<MarketingCoupon> queryList(QueryMarketingCouponVo vo) {

        return getBaseMapper().queryList(vo);
    }

    @Cacheable(value = MarketingCoupon.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public MarketingCoupon findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @OpLog(type = OtherOpLogType.class, name = "新增限时卡券活动表，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String create(CreateMarketingCouponVo vo) {

        MarketingCoupon data = new MarketingCoupon();
        data.setName(vo.getName());
        data.setBeginDate(vo.getBeginDate());
        data.setEndDate(vo.getEndDate());
        data.setCycleFlag(vo.getCycleFlag());
        data.setCycleType(vo.getCycleType());
        data.setCycleTimeGroup(vo.getCycleTimeGroup());
        data.setCycleTimeRule(vo.getCycleTimeRule());
        data.setAllMemberLevel(vo.getAllMemberLevel());
        data.setMemberLevelId(vo.getMemberLevelId());
        data.setAllBuyLimit(vo.getAllBuyLimit());
        data.setCycleBuyLimit(vo.getCycleBuyLimit());
        data.setAllShop(vo.getAllShop());
        data.setShopId(vo.getShopId());
        data.setCouponId(vo.getCouponId());

        getBaseMapper().insert(data);

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);

        return data.getId();
    }

    @OpLog(type = OtherOpLogType.class, name = "修改限时卡券活动表，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(UpdateMarketingCouponVo vo) {

        MarketingCoupon data = getBaseMapper().selectById(vo.getId());
        if (ObjectUtil.isNull(data)) {
            throw new DefaultClientException("限时卡券活动表不存在！");
        }

        LambdaUpdateWrapper<MarketingCoupon> updateWrapper = Wrappers.lambdaUpdate(MarketingCoupon.class)
                .set(MarketingCoupon::getName, vo.getName())
                .set(MarketingCoupon::getBeginDate, vo.getBeginDate())
                .set(MarketingCoupon::getEndDate, vo.getEndDate())
                .set(MarketingCoupon::getCycleFlag, vo.getCycleFlag())
                .set(MarketingCoupon::getCycleType, vo.getCycleType())
                .set(MarketingCoupon::getCycleTimeGroup, vo.getCycleTimeGroup())
                .set(MarketingCoupon::getCycleTimeRule, vo.getCycleTimeRule())
                .set(MarketingCoupon::getAllMemberLevel, vo.getAllMemberLevel())
                .set(MarketingCoupon::getMemberLevelId, vo.getMemberLevelId())
                .set(MarketingCoupon::getAllBuyLimit, vo.getAllBuyLimit())
                .set(MarketingCoupon::getCycleBuyLimit, vo.getCycleBuyLimit())
                .set(MarketingCoupon::getAllShop, vo.getAllShop())
                .set(MarketingCoupon::getShopId, vo.getShopId())
                .set(MarketingCoupon::getCouponId, vo.getCouponId())
                .eq(MarketingCoupon::getId, vo.getId());

        getBaseMapper().update(updateWrapper);

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);
    }

    @CacheEvict(value = MarketingCoupon.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

